package com.xiaoqiu.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoqiu.lease.common.exception.LeaseException;
import com.xiaoqiu.lease.common.result.ResultCodeEnum;
import com.xiaoqiu.lease.model.entity.*;
import com.xiaoqiu.lease.model.enums.ItemType;
import com.xiaoqiu.lease.model.enums.ReleaseStatus;
import com.xiaoqiu.lease.web.admin.mapper.*;
import com.xiaoqiu.lease.web.admin.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoqiu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.xiaoqiu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.xiaoqiu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.xiaoqiu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.xiaoqiu.lease.web.admin.vo.fee.FeeValueVo;
import com.xiaoqiu.lease.web.admin.vo.graph.GraphVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.swing.plaf.nimbus.NimbusLookAndFeel;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo> implements ApartmentInfoService {

    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;



    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        boolean isUpdate = apartmentSubmitVo.getId() != null;
        super.saveOrUpdate(apartmentSubmitVo);
        if (isUpdate) {
            //1.删除图片信息
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper
                    .eq(GraphInfo::getItemId, ItemType.APARTMENT)
                    .eq(GraphInfo::getItemId, apartmentSubmitVo.getId());
            graphInfoService.remove(graphInfoLambdaQueryWrapper);
            //2.删除配套信息
            LambdaQueryWrapper<ApartmentFacility> apartmentFacilityWrapper = new LambdaQueryWrapper<>();
            apartmentFacilityWrapper.eq(ApartmentFacility::getApartmentId, apartmentSubmitVo.getId());
            apartmentFacilityService.remove(apartmentFacilityWrapper);
            //删除标签信息
            LambdaQueryWrapper<ApartmentLabel> labelInfoWrapper = new LambdaQueryWrapper<>();
            labelInfoWrapper.eq(ApartmentLabel::getApartmentId, apartmentSubmitVo.getId());
            apartmentLabelService.remove(labelInfoWrapper);
            //删除杂费信息
            LambdaQueryWrapper<ApartmentFeeValue> feeValueWrapper = new LambdaQueryWrapper<>();
            feeValueWrapper.eq(ApartmentFeeValue::getApartmentId, apartmentSubmitVo.getId());
            apartmentFeeValueService.remove(feeValueWrapper);
        }
        //插入图片信息
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) {
            ArrayList<GraphInfo> graphInfos = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemId(apartmentSubmitVo.getId());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfos.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfos);
        }
        //插入配套信息
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
       if (!CollectionUtils.isEmpty(facilityInfoIds)){
           ArrayList<ApartmentFacility> apartmentFacilities = new ArrayList<>();
           for (Long facilityInfoId : facilityInfoIds) {
               ApartmentFacility apartmentfacility = new ApartmentFacility();
               apartmentfacility.setApartmentId(apartmentSubmitVo.getId());
               apartmentfacility.setFacilityId(facilityInfoId);
               apartmentFacilities.add(apartmentfacility);
           }
           apartmentFacilityService.saveBatch(apartmentFacilities);

       }
        //插入标签信息
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
       if (!CollectionUtils.isEmpty(labelIds)){
           ArrayList<ApartmentLabel> apartmentLabels = new ArrayList<>();
           for (Long labelId : labelIds) {
               ApartmentLabel apartmentLabel = new ApartmentLabel();
               apartmentLabel.setApartmentId(apartmentSubmitVo.getId());
               apartmentLabel.setLabelId(labelId);
               apartmentLabels.add(apartmentLabel);
           }
           apartmentLabelService.saveBatch(apartmentLabels);
       }
        //插入杂费信息
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
       if (!CollectionUtils.isEmpty(feeValueIds)){
           ArrayList<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>();
           for (Long feeValueId : feeValueIds) {
               ApartmentFeeValue apartmentFeeValue = new ApartmentFeeValue();
               apartmentFeeValue.setApartmentId(apartmentSubmitVo.getId());
               apartmentFeeValue.setFeeValueId(feeValueId);
               apartmentFeeValues.add(apartmentFeeValue);
           }
           apartmentFeeValueService.saveBatch(apartmentFeeValues);

       }
    }

    @Override
    public IPage<ApartmentItemVo> pageItem(Page<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
//        LambdaQueryWrapper<ApartmentInfo> apartmentItemVoWrapper = new LambdaQueryWrapper<>();
//        apartmentItemVoWrapper.eq(ApartmentInfo::getIsRelease, ReleaseStatus.RELEASED);
//        List<ApartmentInfo> list = apartmentInfoService.list(apartmentItemVoWrapper);
//
//        for (ApartmentInfo apartmentInfo : list) {
//            ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
//            Long apartmentInfoId = apartmentInfo.getId();
//            LambdaQueryWrapper<RoomInfo> roomInfoWrapper = new LambdaQueryWrapper<>();
//            roomInfoWrapper.eq(RoomInfo::getApartmentId,apartmentInfoId);
//            List<RoomInfo> roomInfos = roomInfoService.list(roomInfoWrapper);
//            Long count = Long.valueOf(roomInfos.size());
//            BeanUtils.copyProperties(apartmentInfo,apartmentItemVo);
//            apartmentItemVo.setTotalRoomCount(count);
//          int free=  roomInfoMapper.getFree(apartmentInfoId);
//        }
        return apartmentInfoMapper.pageApartmentItemByQuery(page, queryVo);
    }

    @Override
    public void removeApartmentById(Long id) {
        LambdaQueryWrapper<RoomInfo> roomQueryWrapper = new LambdaQueryWrapper<>();
        roomQueryWrapper.eq(RoomInfo::getApartmentId, id);
        Long count = roomInfoMapper.selectCount(roomQueryWrapper);
        if (count > 0) {
            throw new LeaseException(ResultCodeEnum.ADMIN_APARTMENT_DELETE_ERROR);
        }
        //1.删除图片信息
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper
                .eq(GraphInfo::getItemId, ItemType.APARTMENT)
                .eq(GraphInfo::getItemId, id);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);
        //2.删除配套信息
        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityWrapper = new LambdaQueryWrapper<>();
        apartmentFacilityWrapper.eq(ApartmentFacility::getApartmentId, id);
        apartmentFacilityService.remove(apartmentFacilityWrapper);
        //删除标签信息
        LambdaQueryWrapper<ApartmentLabel> labelInfoWrapper = new LambdaQueryWrapper<>();
        labelInfoWrapper.eq(ApartmentLabel::getApartmentId, id);
        apartmentLabelService.remove(labelInfoWrapper);
        //删除杂费信息
        LambdaQueryWrapper<ApartmentFeeValue> feeValueWrapper = new LambdaQueryWrapper<>();
        feeValueWrapper.eq(ApartmentFeeValue::getApartmentId, id);
        apartmentFeeValueService.remove(feeValueWrapper);
    }

    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        if (apartmentInfo== null){
            return null;
        }
        //查询图片信息
        LambdaQueryWrapper<GraphInfo> graphInfoQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoQueryWrapper
                .eq(GraphInfo::getItemType,ItemType.APARTMENT)
                .eq(GraphInfo::getItemId,id);
        List<GraphInfo> graphInfoList = graphInfoService.list(graphInfoQueryWrapper);
        List<GraphVo> graphVos=new ArrayList<>();
        for (GraphInfo graphInfo : graphInfoList) {
            GraphVo graphVo = new GraphVo();
            BeanUtils.copyProperties(graphInfo, graphVo);
        graphVos.add(graphVo);
        }
        //查询标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByApartmentId(id);
        //查询配套信息
        List<FacilityInfo> facilityInfoList= facilityInfoMapper.selectListByApartmentId(id);
        //查询杂费信息
        List<FeeValueVo> feeValueVoList =feeValueMapper.selectListByApartmentId(id);
        //组装结果
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        apartmentDetailVo.setGraphVoList(graphVos);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        return apartmentDetailVo;
    }

}




